<template>
  <div class="detail-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">

          <el-tab-pane label="用户管理" name="first">
            <el-form ref="ruleForm" :model="ruleForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">

              <el-form-item label="姓名" prop="username">
                <el-input v-model="ruleForm.username" autocomplete="off" style="width:25%" />
              </el-form-item>
              <el-form-item label="更改密码" prop="newPassword">
                <el-input v-model="ruleForm.newPassword" type="password" autocomplete="off" style="width:25%" />
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button>重置</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <el-tab-pane label="配置管理" name="second">
            <el-tooltip class="tooltip-box" content="打印基本个人信息">
              <router-link :to="`/employees/print/${$route.params.id}?type=personal`">
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="角色管理" name="third">
            <el-tooltip class="tooltip-box" content="打印岗位的个人信息">
              <router-link :to="`/employees/print/${$route.params.id}?type=personal`">
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqGetUserDetailById } from '@/api/user'
import { reqSaveUserDetailById } from '@/api/employees'
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
export default {
  name: 'Details',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      activeName: 'first',
      ruleForm: {
        username: '',
        newPassword: ''

      },
      rules: {
        username: [
          { required: true, message: '请输入用户名' },
          { max: 4, message: '用户名为1-4位' }
        ],
        newPassword: [
          { required: true, message: '请输入密码' },
          { min: 4, max: 8, message: '用户名为4-8位' }
        ]
      },
      getUserDetailById: {}

    }
  },
  async created() {
    const res = await reqGetUserDetailById(this.$route.params.id)
    this.getUserDetailById = res
    this.ruleForm.username = res.username
  },
  methods: {

    submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          await reqSaveUserDetailById({
            ...this.getUserDetailById,
            username: this.ruleForm.username,
            password: this.ruleForm.newPassword
          })
          this.$message.success('更改成功')
          if (this.getUserDetailById.id === this.$store.state.user.userInfo.id) {
            this.$store.dispatch('user/removeTokenAndUserInfo')
            this.$router.push('/login')
          }
        } else {
          console.log(this.getUserDetailById.id === this.$store.state.user.userInfo.id)
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style>

</style>
